<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" border v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column label="设备名称" align="center" prop="equipmentName" width="180px" />
      <el-table-column label="开始时间" align="center" prop="startTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="结束时间" align="center" prop="endTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="维修时长(分钟)" align="center" prop="repairDuration" />
      <el-table-column label="维修成本(元)" align="center" prop="repairCost" />
      <el-table-column label="是否解决" align="center" prop="resolved" />
      <el-table-column label="故障分析" align="center" prop="faultAnalysis" />
      <el-table-column label="维修事项" align="center" prop="repairItem" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" fixed="right" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openViewDialog(scope.row.repairId)"
            v-hasPermi="['business:repair-task:update']">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 维修申请单详细信息 -->
  <RepairTaskInfoView ref="viewRef" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { RepairTaskApi, RepairTask } from '@/api/business/repairtask'
import RepairTaskForm from './RepairTaskForm.vue'
import RepairTaskInfoView from './RepairTaskInfoView.vue'

/** 维修任务 列表 */
defineOptions({ name: 'RepairTask' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<RepairTask[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  maintenanceId: undefined,
  equipmentId: undefined,
  createTime: [],

})

//维修详细信息
const viewRef = ref()

//维修申请详细信息
const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id, true)
}

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  await nextTick()
  try {
    const data = await RepairTaskApi.getRepairTaskPage(queryParams)
    console.log("data", data);

    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RepairTaskApi.deleteRepairTask(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除维修任务 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await RepairTaskApi.deleteRepairTaskList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: RepairTask[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RepairTaskApi.exportRepairTask(queryParams)
    download.excel(data, '维修任务.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

//初始化数据
const initData = (row) => {
  console.log('子组件', row);
  queryParams.equipmentId = row.equipmentId
  getList()
}

defineExpose({ initData }) // 提供 open 方法，用于打开弹窗

/** 初始化 **/
onMounted(() => {
  // getList()
})
</script>
